package com.base.framework.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StringKit;
/**
 * 
 * PropertiesUtil
 * TODO 加载配置文件（参考Jfinal中的JFinalConfig）
 * @author 罗盛力
 * 2013-9-8 下午5:04:52
 */
public class PropertiesUtil extends Properties{
	private static final long serialVersionUID = 4438315768553802904L;
	private static Properties properties;

	/**
	 * 加载配置文件 e.g 1: loadPropertyFile("db_username_pass.txt"); e.g 2:
	 * loadPropertyFile("config/db_username_pass.txt");
	 * 
	 * @param 文件需要在WEB
	 *            -INF目录下
	 */
	public static Properties loadPropertyFile(String file) {
		if (StringKit.isBlank(file))
			throw new IllegalArgumentException(
					"Parameter of file can not be blank");
		if (file.contains(".."))
			throw new IllegalArgumentException(
					"Parameter of file can not contains \"..\"");

		InputStream inputStream = null;
		String fullFile; // String fullFile = PathUtil.getWebRootPath() + file;
		if (file.startsWith(File.separator))
			fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF"
					+ file;
		else
			fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF"
					+ File.separator + file;

		try {
			inputStream = new FileInputStream(new File(fullFile));
			Properties p = new Properties();
			p.load(inputStream);
			properties = p;
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException("Properties file not found: "
					+ fullFile);
		} catch (IOException e) {
			throw new IllegalArgumentException(
					"Properties file can not be loading: " + fullFile);
		} finally {
			try {
				if (inputStream != null)
					inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (properties == null)
			throw new RuntimeException("Properties file loading failed: "
					+ fullFile);
		return properties;
	}

	public String getProperty(String key) {
		checkPropertyLoading();
		return properties.getProperty(key);
	}

	public String getProperty(String key, String defaultValue) {
		checkPropertyLoading();
		return properties.getProperty(key, defaultValue);
	}

	public Integer getPropertyToInt(String key) {
		checkPropertyLoading();
		Integer resultInt = null;
		String resultStr = properties.getProperty(key);
		if (resultStr != null)
			resultInt = Integer.parseInt(resultStr);
		return resultInt;
	}

	public Integer getPropertyToInt(String key, Integer defaultValue) {
		Integer result = getPropertyToInt(key);
		return result != null ? result : defaultValue;
	}

	public Boolean getPropertyToBoolean(String key) {
		checkPropertyLoading();
		String resultStr = properties.getProperty(key);
		Boolean resultBool = null;
		if (resultStr != null) {
			if (resultStr.trim().equalsIgnoreCase("true"))
				resultBool = true;
			else if (resultStr.trim().equalsIgnoreCase("false"))
				resultBool = false;
		}
		return resultBool;
	}

	public Boolean getPropertyToBoolean(String key, boolean defaultValue) {
		Boolean result = getPropertyToBoolean(key);
		return result != null ? result : defaultValue;
	}

	private void checkPropertyLoading() {
		if (properties == null)
			throw new RuntimeException(
					"You must load properties file by invoking loadPropertyFile(String) method in configConstant(Constants) method before.");
	}

}
